package cn.iocoder.yudao.module.blog.service.improvesection;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.improvesection.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.improvesection.BlogImproveSectionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.improvesection.BlogImproveSectionConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.improvesection.BlogImproveSectionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 课程章节 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class BlogImproveSectionServiceImpl implements BlogImproveSectionService {

    @Resource
    private BlogImproveSectionMapper improveSectionMapper;

    @Override
    public Long createImproveSection(BlogImproveSectionCreateReqVO createReqVO) {
        // 插入
        BlogImproveSectionDO improveSection = BlogImproveSectionConvert.INSTANCE.convert(createReqVO);
        improveSectionMapper.insert(improveSection);
        // 返回
        return improveSection.getId();
    }

    @Override
    public void updateImproveSection(BlogImproveSectionUpdateReqVO updateReqVO) {
        // 校验存在
        validateImproveSectionExists(updateReqVO.getId());
        // 更新
        BlogImproveSectionDO updateObj = BlogImproveSectionConvert.INSTANCE.convert(updateReqVO);
        improveSectionMapper.updateById(updateObj);
    }

    @Override
    public void deleteImproveSection(Long id) {
        // 校验存在
        validateImproveSectionExists(id);
        // 删除
        improveSectionMapper.deleteById(id);
    }

    private void validateImproveSectionExists(Long id) {
        if (improveSectionMapper.selectById(id) == null) {
            throw exception(IMPROVE_SECTION_NOT_EXISTS);
        }
    }

    @Override
    public BlogImproveSectionDO getImproveSection(Long id) {
        return improveSectionMapper.selectById(id);
    }

    @Override
    public List<BlogImproveSectionDO> getImproveSectionByImproveId(Long id){
        return improveSectionMapper.selectList(BlogImproveSectionDO::getImproveId, id);
    }

    @Override
    public List<BlogImproveSectionDO> getImproveSectionByImproveIds(Collection<Long> ids){
     return improveSectionMapper.selectList(BlogImproveSectionDO::getImproveId, ids);
    }

    @Override
    public List<BlogImproveSectionDO> getImproveSectionList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return improveSectionMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlogImproveSectionDO> getImproveSectionPage(BlogImproveSectionPageReqVO pageReqVO) {
        return improveSectionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlogImproveSectionDO> getImproveSectionList(BlogImproveSectionExportReqVO exportReqVO) {
        return improveSectionMapper.selectList(exportReqVO);
    }

}
